package com.G3.Screen;

import java.util.Random;

import com.G3.Base.MyImage;
import com.G3.Global.Global;
import com.G3.Global.OverLapTester;
import com.G3.Object.Boom;
import com.G3.Object.Gift;
import com.G3.Object.Opponent;
import com.G3.Object.Player;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.utils.Array;

public class GameScreen extends BaseScreen{

	private static final float RADIUS_EFFECT_BOUND_OF_PLAYER = 300;
	
	public boolean isOver = false;
	public boolean isWin = false;
	public boolean isPause = false;
	Array<Gift> lstGift = new Array<Gift>();
	Array<Boom> lstBoom = new Array<Boom>();
	int nMaxBoom;
	int maxLiveOfOpponent = 1;
	Player player;
	public static int levle = 0;
	public static int score = 0;
	int maxOpponent = 10;
	float maxVOpponent;
	Array<Opponent> lstOpponent = new Array<Opponent>();
	Group group;
	TextureRegion backgroundRegion;
	enum STATE
	{
		PLAYING,
	};
	STATE stateGame = STATE.PLAYING;
	
	public GameScreen(Game game, int level) {
		super(game);
		this.levle = level;
		initLevel();
		createWorld();
	}
	
	public void initLevel()
	{
		backgroundRegion = atlas.findRegion("background", 1);
	}
	
	private void createWorld() {
		group = new Group();
		stage.clear();
		stage.addActor(group);
		MyImage background = new MyImage(new Vector2(Global.VIEWPORT_WIDTH/2, Global.VIEWPORT_HEIGHT/2), backgroundRegion);
		group.addActor(background);
		player = Player.getInstrance(new Vector2(400, 240));
		group.addActor(player);
	}

	@Override
	public void update(float delta) {
		super.update(delta);
		
		if(stateGame == STATE.PLAYING)
		{
			updatePlaying(delta);
		}
	}
	
	
	
	private void updatePlaying(float delta) {
		for(Opponent o : lstOpponent)
		{
			o.moveToAction(player.getCenterPosition().x, player.getCenterPosition().y);
		}
		if(Gdx.input.isTouched())
		{
			Vector2 touch = Global.getTouchPosition();
			player.moveToAction(touch.x, touch.y);
		}
		if(lstOpponent.size < 10)
		{
			addAOpponent();
		}
		
		for(int i = lstOpponent.size - 1; i >= 0; i--)
		{
			if(OverLapTester.overlapCircles(lstOpponent.get(i).getBound(), player.getBound()))
			{
				killOpponent(lstOpponent.get(i));
			}
		}
	}
	
	void killOpponent(Opponent opp)
	{
		opp.killed();
		if(opp.isDie())
		{
			lstOpponent.removeValue(opp, true);
			group.removeActor(opp);
		}
	}
	
	public void addAOpponent()
	{
		Opponent opp = new Opponent(getRandomStartPositionOpponent());
		lstOpponent.add(opp);
		group.addActor(opp);
	}
	
	public Vector2 getRandomStartPositionOpponent()
	{
		Vector2[] lstStartPosition = new Vector2[4];
		Random r = new Random();
		lstStartPosition[0] = new Vector2(0, r.nextInt(Global.VIEWPORT_HEIGHT));// left
		lstStartPosition[1] = new Vector2(Global.VIEWPORT_WIDTH, r.nextInt(Global.VIEWPORT_HEIGHT));// right
		lstStartPosition[2] = new Vector2(r.nextInt(Global.VIEWPORT_WIDTH), 0);// botton
		lstStartPosition[3] = new Vector2(r.nextInt(Global.VIEWPORT_WIDTH), r.nextInt(Global.VIEWPORT_HEIGHT));// top
		
		Circle playerEffectBound = new Circle(player.getCenterPosition(), RADIUS_EFFECT_BOUND_OF_PLAYER);
		Vector2 position = lstStartPosition[r.nextInt(lstStartPosition.length)];
		if(OverLapTester.pointInCircle(playerEffectBound, position))
		{
			return getRandomStartPositionOpponent();
		}
		return position;
	}

	@Override
	public void render(float delta) {
		super.render(delta);
	}
}
